<?php
namespace app\home\controller;

use think\Db;
use think\Controller;
use think\Request;
use think\Session;
use think\Cookie;

class Base extends Controller{

	public function _initialize() {
		$this->isLogin();
        $this->getNode();
	}

    public function isLogin() {
    	$u_id = session("u_id");
    	if(!$u_id){
    		$this->error("请先登录....","home/login/login");
    	}
    }

    public function getNode() {
        $u_id = session("u_id");

        $sql_1 = "select r_id from agent_user_role where u_id = $u_id";//根据用户u_id查所有的角色r_id
        $sql_2 = "select n_id from agent_role_node where r_id in ($sql_1)";//根据所有的r_id查所有的权限n_id
        $sql_3 = "select * from agent_node where n_id in ($sql_2)";//根据所有的n_id查所有的权限具体数据

        $userNodes = Db::query($sql_3);
        // print_r($userNodes);die;
        //先取出所有的父权限
        $p_arr = array();
        foreach ($userNodes as $key => $value) {
            if($value['p_id'] == 0){
                $p_arr[] = $value;
            }
        }
        
        //找各自对应的子权限
        foreach ($p_arr as $key => $value) {
            foreach ($userNodes as $k => $v) {
                if($value['n_id'] == $v['p_id']){
                    $p_arr[$key]['son'][] = $v;
                }
            }
        }

        
        // print_r($p_arr);die;

        $this->assign("userNodes",$p_arr);
        return view('index');


    }
}